/**
 * 游戏对象
 */
//+function ($) {
//	"use strict";
	var user = new User();
	var computer = new User();
	var poke = Poke;
	
	var Game = {
		
		//游戏开始
		start:function(){
			//1、生成52张牌，并清空原先的
			poke.init();
			user.reset();
			computer.reset();
			
			var $untreated = $("#untreated");
			var $computer = $("#computer");
			var $user = $("#user");
			for(var i=0;i<poke.cards.length;i++){
				var card = poke.cards[i];
				var $img = $("<img>");
//				$img.attr("src",Card.prototype.imagPpreffix + card.ico);
				$img.attr("src",Card.prototype.imagPpreffix + "backSide.png");
				$img.addClass("backside");
//				$img.addClass("img-responsive");
				$img.css({"margin-left":i * 10 + "px"});
				
				$img.appendTo($untreated);
			}
			$user.empty();
			$computer.empty();
			
			//2、电脑和玩家各发两个牌
			this._addCard($user,true,user);
			this._addCard($user,false,user);
			this._addCard($computer,true,computer);
			this._addCard($computer,false,computer);
			
			//3、玩家显示牌面
		},
		
		//玩家发牌
		userAddCard:function(){
			//1、把前面一张显示
			var $user = $("#user");
			this._displayCard($user, user);
			//2、发牌
			this._addCard($user, false, user);
			//3、计算下结果，如果超过21点，就不能再发
		},
		
		//计算牌面总分
		getSum:function(who){
			var total = 0;
			var countA = 0;
			var cards = who.cards;
			var length = cards.length;
			if(length){
				for(var i=0;i<length;i++){
					var card = cards[i];
					if(card.name=="A"){
						countA++;
					}else{
						total+=card.value;
					}
				}
				if(countA){
					if(total+countA<=11){
						total+=(countA+10);
					}else{
						total+=countA;
					}
				}
			}
			return total;
		},
		
		//开牌
		showCards:function(){
			//1、显示玩家的电脑的牌面
			this._displayCard($("#computer"), computer);
			this._displayCard($("#user"), user);
			//2、计算两个总和
			var computerTotal = this.getSum(computer);
			var userTotal = this.getSum(user);
			//3、比较
			var strResult = "";
			if(userTotal > 21){
				strResult = "电脑胜";
			}else{
				if(computerTotal > 21){
					strResult = "玩家胜";
				}else{
					if(computerTotal > userTotal){
						strResult = "电脑胜";
					}else if(computerTotal == userTotal){
						strResult = "平局";
					}else{
						strResult = "玩家胜";
					}
				}
			}
			$("#result").html("<h3>" + strResult + "</h3>");
		},
		
		//游戏结束
		end:function(){
			window.close();
		},
		
		//增加一张牌
		_addCard:function($div,bShow,who){
			//1、增加一张牌，并显示
			//2、发牌区减少一张
			var card = poke.deal();
			var $img = $("<img>");
			if(!bShow){
				$img.attr("src",Card.prototype.imagPpreffix + "backSide.png");
			}else{
				$img.attr("src",Card.prototype.imagPpreffix + card.ico);
			}
			$img.appendTo($div);
			who.cards.push(card);
//			$("#untreated").last().remove();
		},
		
		//显示玩家的牌面
		_displayCard:function($div,who){
			var card = who.cards[who.cards.length-1];
			$div.find("img:last-child").attr("src",Card.prototype.imagPpreffix + card.ico);
		},

	};
//}(window.jQuery);
